package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/baseinfo/factory")
public class FactoryController extends BaseController {

    @Reference(check = false,retries = 0)
    private FactoryService factoryService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        //创建查询添加
        FactoryExample factoryExample = new FactoryExample();
        //创建条件对象
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        PageInfo<Factory> pageInfo = factoryService.findByPage(factoryExample, pageNum, pageSize);
        //存储到请求域中
        request.setAttribute("pageInfo",pageInfo);
        return "baseinfo/factory/factory-list";
    }

    /*
    作用：进入工厂的添加
    路径： /cargo/factory/toAdd.do
    参数： 没有
    返回值： cargo/factory/factory-add
 */
    @RequestMapping("/toAdd")
    public String toAdd(){
         return "baseinfo/factory/factory-add";
    }


    /*
    作用： 保存添加与修改工厂
    路径： /cargo/factory/edit.do
    参数： 工厂
    返回值：工厂列表
 */
    @RequestMapping("/edit")
    public String edit(Factory factory){

        //2.根据工厂的id判断是否是增加还是修改
        if(StringUtils.isEmpty(factory.getId())){
            //添加
            factoryService.save(factory);
        }else{
            //修改
            factoryService.update(factory);
        }
        //3. 返回工厂的列表页面
        return "redirect:/baseinfo/factory/list.do";
    }


    /*
       作用：进入修改页面
       路径： /cargo/factory/toUpdate.do?id=5af45318-6585-4e8b-85c6-75c1f3bf2c9f
       参数： 工厂的id
       返回值：cargo/factory/factory-update
    */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //1. 根据id查询当前工厂
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);
        //2. 返回更新页面
        return "baseinfo/factory/factory-update";
    }

    /*
         作用：删除工厂
         路径： /cargo/factory/delete.do
         参数： 工厂的id
         返回值：工厂列表
     */
    @RequestMapping("/delete")
    public String delete(String id){
        //1. 删除工厂
         factoryService.delete(id);
        return "redirect:/baseinfo/factory/list.do";
    }






}
